import lombok.extern.slf4j.Slf4j;

import java.util.Vector;
import java.util.concurrent.locks.LockSupport;

/**
 * 检验 对象头markwork 偏向锁的批量撤销阈值40
 *
 * @author 管子
 * @date 2025/4/23 19:28
 * @description: TestBiased 类
 */
@Slf4j
public class TestBiased {

    static Thread t1, t2, t3;

    public static void main(String[] args) {
        test4();
    }

    private static void test4() {
        Vector<Object> list = new Vector<>();
        int loopNumber = 39;
        t1 = new Thread(() -> {
            for (int i = 0; i < loopNumber; i++) {
                Object o = new Object();
                list.add(o);
                synchronized (o) {
                    log.info(i + "\t" + "markword");
                }
            }
            LockSupport.unpark(t2);
        }, "t1");
        t1.start();

        t2 = new Thread(() -> {
            LockSupport.park();
            for (int i = 0; i < loopNumber; i++) {
                Object o = list.get(i);
                synchronized (o) {
                    log.info(i + "\t" + "markword");
                }
            }
            LockSupport.unpark(t3);
        }, "t2");
        t2.start();

        t3 = new Thread(() -> {
            LockSupport.park();
            for (int i = 0; i < loopNumber; i++) {
                Object o = list.get(i);
                synchronized (o) {
                    log.info(i + "\t" + "markword");
                }
            }
        }, "t3");
        t3.start();

    }

}
